package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.Vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-04
 */
@Service
public class NursingProjectServiceImpl  extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingProject:all";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        nursingProject.setCreateTime(DateUtils.getNowDate());
        int flag = nursingProjectMapper.insertNursingProject(nursingProject);
        deleteCache();
        return flag ;
    }
    private void deleteCache() {
        // 删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }
    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        nursingProject.setUpdateTime(DateUtils.getNowDate());
        int flag = nursingProjectMapper.updateNursingProject(nursingProject);
        deleteCache();
        return flag;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int flag = nursingProjectMapper.deleteNursingProjectByIds(ids);
        deleteCache();
        return flag;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingProject> selectAll() {
//        return nursingProjectMapper.selectAll();
        //从缓存中查找
        List<NursingProject> nursingProjects = (List<NursingProject>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        // 如果缓存中存在，直接返回
        if (nursingProjects != null && nursingProjects.size() > 0) {
            return nursingProjects;
        }
        // 缓存中不存在，从数据库中查询
        nursingProjects = nursingProjectMapper.selectAll();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, nursingProjects);
        return nursingProjects;
    }
}
